@ SWI_handler.S
@
@

	.file	"SWI_handler.S"
	.text
	
	.global SWI_handler

SWI_handler:
	
	@ Store all registers on the stack
	STMFD	sp!,	{r1-r12,lr}
	
	@ Get the SWI number
	LDR	r4,	[lr, #-4]
	BIC	r4, r4, #0xFF000000
	
	@ Check if it's "exit"
	LDR	r5,  =0x900001
	CMP	r4, r5
	BEQ	exit_to_kernel

read_h:
	@ Check if it's "read"
	LDR	r5,  =0x900003
	CMP	r4, r5
	BNE     write_h  
	BLEQ	read
	B	end    	

write_h:
	@ Check if it's "write"
	LDR	r5,  =0x900004
	CMP	r4, r5          
	BNE	invalid         
 	BLEQ	write           
	B	end

time_h:
	@ Check if it's "time"
	LDR	r5,  =0x900006
	CMP	r4, r5          
	BNE	invalid         
 	BLEQ	write           
	B	end

sleep_h:
	@ Check if it's "sleep"
	LDR	r5,  =0x900007
	CMP	r4, r5          
	BNE	invalid         
 	BLEQ	write           
	B	end




	@ In case of invalid SWI number
	@ print out error message and exit
invalid:
	LDR	r0, message
	BL	puts          
	LDR	r0, =0x0badc0de
	B	exit_to_kernel

end:
	@ Store user program registers
	@ and return with mode changed
	LDMFD	sp!, {r1-r12, lr}
	MOVS	pc, lr

	
message:
	.word		err_mes
	.section 	.rodata
	
err_mes:
	.ascii	"Invalid Syscall \n"
